package com.kefu.call.task;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kefu.call.model.ContactPlan;
import com.kefu.call.service.ContactPlanService;
import com.kefu.common.util.JacksonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Component
@ConditionalOnProperty(prefix = "kefu.task.execute", name = "enabled", havingValue = "true", matchIfMissing = true)
public class ContactPlanTask {
    @Resource
    private ContactPlanService contactPlanService;

    /**
     * 联系计划定时任务
     */
    @Scheduled(cron = "${kefu.call.plan.invalid.cron:0 5 0 * * ? }")
    public void invalid() {
        List<ContactPlan> list = contactPlanService.list(Wrappers.lambdaQuery(ContactPlan.class));
        list.forEach(v -> {
            Long subscribeTime = v.getSubscribeTime();
            if (DateUtil.current() < subscribeTime) {
                log.info("联系计划已超时: {}", JacksonUtil.toString(v));
            }
        });
    }
}
